import threading
import time

# def thread1():
#     print('子线程1id', threading.get_ident())  # 子线程1
#     while True:
#         print(threading.get_ident(), 2)
#         time.sleep(1)
#
#
# def thread3(num):
#     print('子线程3开始', threading.get_ident())  # 子线程3
#     while True:
#         print(threading.get_ident(), num)
#         time.sleep(1)
#
#
# def main():
#     print('主进程开始', threading.get_ident())  # 主进程
#     t1 = threading.Thread(target=thread1)
#     t1.start()
#     for i in range(3, 5):
#         t3 = threading.Thread(target=thread3, args=(i,))
#         t3.start()
#     while True:
#         print(threading.get_ident(), 1)
#         time.sleep(1)
#
#
# if __name__ == '__main__':
#     main()


import multiprocessing
import requests

cookies = {
    'buvid3': 'C7D9D56D-355B-6058-0238-3E944A92C4B962224infoc',
    'b_nut': '1721208762',
    '_uuid': 'D1039102110-43710-FE310-AD5F-5BF593865EC1063225infoc',
    'buvid_fp': '42ff0e13e0d511c966aa25e74edbd959',
    'enable_web_push': 'DISABLE',
    'buvid4': 'C24F625E-D5A8-546F-876C-F432350EB4F563884-024071709-eypfGO2HhAiT1TL8JCbQXw%3D%3D',
    'rpdid': "|(k|~RuRukl~0J'u~kuk|))|)",
    'header_theme_version': 'CLOSE',
    'DedeUserID': '2116722009',
    'DedeUserID__ckMd5': 'd0ab7aedec240dc2',
    'CURRENT_QUALITY': '80',
    'CURRENT_FNVAL': '4048',
    'is-2022-channel': '1',
    'bp_t_offset_2116722009': '992125359338029056',
    'bsource': 'search_baidu',
    'home_feed_column': '5',
    'browser_resolution': '1490-794',
    'b_lsid': '5CB45F3E_192F507EE0C',
    'SESSDATA': 'f124a9cc%2C1746239789%2C50e95%2Ab1CjDpVfxjeD8KMFqI77owNAIjOsBEJlK1ak5PGB9N-Abr1aiLYbKXDDcK2O3j_LP6YBwSVkJpODUycVN0VUhpVUkxcG51SmlLMTR6aG5QdExHazFHWXphX1FGcWk2YnR5eWxnU2prRUx6TWx1QTl1aEF1d2FBWlR1VXRCRnItRE85UThjbDZwanVnIIEC',
    'bili_jct': 'f94fdaa6ecd4d7b9e0a688ffc3d9635a',
    'sid': '6pk27eeu',
    'bili_ticket': 'eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzA5NDc5NjUsImlhdCI6MTczMDY4ODcwNSwicGx0IjotMX0.pmQ3jzYfXtq3dRpyA5UMt7gh54THNAlg1g2G7ehWW9o',
    'bili_ticket_expires': '1730947905',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    # 'cookie': "buvid3=C7D9D56D-355B-6058-0238-3E944A92C4B962224infoc; b_nut=1721208762; _uuid=D1039102110-43710-FE310-AD5F-5BF593865EC1063225infoc; buvid_fp=42ff0e13e0d511c966aa25e74edbd959; enable_web_push=DISABLE; buvid4=C24F625E-D5A8-546F-876C-F432350EB4F563884-024071709-eypfGO2HhAiT1TL8JCbQXw%3D%3D; rpdid=|(k|~RuRukl~0J'u~kuk|))|); header_theme_version=CLOSE; DedeUserID=2116722009; DedeUserID__ckMd5=d0ab7aedec240dc2; CURRENT_QUALITY=80; CURRENT_FNVAL=4048; is-2022-channel=1; bp_t_offset_2116722009=992125359338029056; bsource=search_baidu; home_feed_column=5; browser_resolution=1490-794; b_lsid=5CB45F3E_192F507EE0C; SESSDATA=f124a9cc%2C1746239789%2C50e95%2Ab1CjDpVfxjeD8KMFqI77owNAIjOsBEJlK1ak5PGB9N-Abr1aiLYbKXDDcK2O3j_LP6YBwSVkJpODUycVN0VUhpVUkxcG51SmlLMTR6aG5QdExHazFHWXphX1FGcWk2YnR5eWxnU2prRUx6TWx1QTl1aEF1d2FBWlR1VXRCRnItRE85UThjbDZwanVnIIEC; bili_jct=f94fdaa6ecd4d7b9e0a688ffc3d9635a; sid=6pk27eeu; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzA5NDc5NjUsImlhdCI6MTczMDY4ODcwNSwicGx0IjotMX0.pmQ3jzYfXtq3dRpyA5UMt7gh54THNAlg1g2G7ehWW9o; bili_ticket_expires=1730947905",
    'origin': 'https://www.bilibili.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.bilibili.com/v/douga/mad/',
    'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
}


def thread1(datas):
    while True:
        if len(datas) > 0:
            url = datas.pop()
            response = requests.get(url, cookies=cookies,headers=headers)
            print(threading.get_ident(), response.json())
        else:
            break
    print(f"进程结束", threading.get_ident())


def main():
    print('主进程开始', multiprocessing.current_process().ident)  # 主进程
    datas = [
        f"https://api.bilibili.com/x/web-interface/newlist_rank?main_ver=v3&search_type=video&view_type=hot_rank&copy_right=-1&new_web_tag=1&order=click&cate_id=24&page={page}&pagesize=30&time_from=20241028&time_to=20241104"
        for page in range(1, 20)]
    thread_list = []
    for i in range(4):
        t = threading.Thread(target=thread1, args=(datas,))

        thread_list.append(t)
        t.start()

    for t in thread_list:
        t.join()
    print('主进程结束')  # 主进程


if __name__ == '__main__':
    main()
